

            


            
using System;
using System.Collections.Generic;
using System.Web;
using System.Linq;
namespace DatabaseComponent
{
     public partial class Persons
     {




         private List<ManagerEmployees> _lstMyEmployeesManagerEmployees = null;

         public List<ManagerEmployees> lstMyEmployeesManagerEmployees
         {
             get {

                 ManagerEmployeesDB objManagerEmployeesDB = new ManagerEmployeesDB(ConnectionStrings.LMS);

                 _lstMyEmployeesManagerEmployees = objManagerEmployeesDB.getManagerEmployeesByManagerID(GenID);
                    
                 
                 
                 
                 
                 
                 return _lstMyEmployeesManagerEmployees; }
             
         }




         private List<ManagerEmployees> _lstMyManagersManagerEmployees = null;

         public List<ManagerEmployees> lstMyManagersManagerEmployees
         {
             get
             {

                 ManagerEmployeesDB objManagerEmployeesDB = new ManagerEmployeesDB(ConnectionStrings.LMS);

                 _lstMyManagersManagerEmployees = objManagerEmployeesDB.getManagerEmployeesByEmployeeID (GenID);






                 return _lstMyManagersManagerEmployees;
             }

         }




            //Write any additional methods or properties here 
            //Don't modify any genrated classes

         public static long MyCount
         {
             get
             {
                 PersonsDB objPersonsDB = new PersonsDB(ConnectionStrings.LMS);

                 return objPersonsDB.getPersonCount();
             }
         }



         private UserType _objUserType = null;

         public UserType objUserType
         {
             get {
                 if (_objUserType == null)
                 {
                     UserTypeDB objUserTypeDB = new UserTypeDB(ConnectionStrings.LMS);
                     List<UserType> lstUserType = objUserTypeDB.getUserTypeByID(UserTypeID);
                     if (lstUserType.Count == 0)
                         _objUserType = new UserType ();
                     else
                         _objUserType = lstUserType[0];
                 }

                 return _objUserType;
                    
             }
             
         }



         private PersonType _objPersonType = null;

         public PersonType objPersonType
         {
             get {
                 if (_objPersonType == null)
                 {
                     if (PersonTypeID != null)
                     {
                         PersonTypeDB objPersonTypeDB = new PersonTypeDB(ConnectionStrings.LMS);
                         List<PersonType> lstPersonType = objPersonTypeDB.getPersonTypeByID(PersonTypeID.Value);
                         if (lstPersonType.Count == 0)
                             _objPersonType = new PersonType ();
                         else
                             _objPersonType = lstPersonType[0];
                     }
                     else
                         _objPersonType = new PersonType();
                 }
                        
                 
                 
                 
                 return _objPersonType; 
             }
             
         }


         private GenderValue _objGenderValue = null;

         public GenderValue objGenderValue
         {
             get {

                 if (_objGenderValue == null)
                 {
                     GenderValueDB objGenderValueDB = new GenderValueDB(ConnectionStrings.LMS);
                     List<GenderValue> lstGenderValue = objGenderValueDB.getGenderValueByGenderID(Gender);
                     if (lstGenderValue.Count == 0)
                     {
                         _objGenderValue = new GenderValue ();
                     }
                     else
                     {
                         _objGenderValue = lstGenderValue[0];
                     }
                 }
                 
                 
                 
                 return _objGenderValue; }
             
         }



         private Relation _objRelation = null;

         public Relation objRelation
         {
             get {

                 if (_objRelation == null)
                 {
                     RelationDB objRelationDB = new RelationDB(ConnectionStrings.LMS);
                     List<Relation> lstRelation = objRelationDB.getRelationByID (RelationID.Value);
                     if (lstRelation.Count == 0)
                         _objRelation = new Relation ();
                     else
                         _objRelation = lstRelation[0];
                 }
                 
                 
                 
                 return _objRelation; }
             
         }



         Persons _objParent = null;

         public Persons objParent
         {
             get {

                 if (PersonTypeID != 3)
                 {
                     return new Persons();
                 }

                 if (_objParent == null)
                 {
                     PersonsDB objPersonsDB = new PersonsDB(ConnectionStrings.LMS);
                     List<Persons> lstPersons = objPersonsDB.getPersonsByBadge(Badge);
                     lstPersons = new List<Persons>(from objPers in lstPersons where objPers.GenID != GenID select objPers);
                     if (lstPersons.Count == 0)
                         return new Persons();

                     _objParent = lstPersons[0];



                 }

                 
                 
                 
                 
                 
                 return _objParent; }
             
         }


         List<Persons> _lstChilds = null;

         public List<Persons> lstChilds
         {
             get {

                 if (PersonTypeID == 3)
                 {
                     return new List<Persons>();
                 }

                 PersonsDB objPersonsDB = new PersonsDB(ConnectionStrings.LMS);
                 List<Persons> lstPersons = objPersonsDB.getPersonsByBadge(Badge);
                 lstPersons = new List<Persons>(from objPers in lstPersons where objPers.GenID != GenID select objPers);

                 if (lstPersons.Count == 0)
                 {
                     return lstPersons;
                 }

                 _lstChilds = lstPersons;
                 
                 
                 return _lstChilds; }
             
         }

         /// <summary>
         /// this function will return the full name of the employee
         /// </summary>
         /// <returns>a string containing the fullname</returns>
         public string getFullName()
         {
             if (Name_EN.Equals(String.Empty) && MiddleName_EN.Equals(String.Empty) && FamilyName_EN.Equals(String.Empty))
                 return "";

             return Name_EN + " " + MiddleName_EN + " " + FamilyName_EN;
         }


         /// <summary>
         /// this function will return the full name of the employee
         /// </summary>
         /// <returns>a string containing the fullname</returns>
         public string getFullNameAR()
         {
             if (Name_AR.Equals(String.Empty) && MiddleName_AR.Equals(String.Empty) && FamilyName_AR.Equals(String.Empty))
                 return "";

             return Name_AR + " " + MiddleName_AR + " " + FamilyName_AR;
         }






         List<Groups> _lstGroups = null;

         public List<Groups> lstGroups
         {
             get {

                 if (_lstGroups == null)
                 {
                     PersonGroupsDB objPersonGroupsDB = new PersonGroupsDB(ConnectionStrings.LMS);

                     List<PersonGroups> lstPersonGroups = objPersonGroupsDB.getPersonGroupsByPersonsID(GenID);

                     if (lstPersonGroups.Count == 0)
                         return new List<Groups>();


                     _lstGroups = new List<Groups> ();
                     foreach (PersonGroups pg in lstPersonGroups)
                     {
                         if (pg.objGroups.ID != -1)
                             _lstGroups.Add(pg.objGroups);
                     }
                     
                 }
                 
                 
                 
                 
                 
                 return _lstGroups; }
            
         }


         List<Rules> _lstRules = null;

         public List<Rules> lstRules
         {
             get {

                 if (_lstRules == null)
                 {

                     PersonsRulesDB objPersonsRulesDB = new PersonsRulesDB(ConnectionStrings.LMS);
                     List<PersonsRules> lstPersonsRules = objPersonsRulesDB.getPersonsRulesByPersonsID(GenID);

                     if (lstPersonsRules.Count == 0)
                         return new List<Rules>();


                     _lstRules = new List<Rules>();

                     foreach (PersonsRules pr in lstPersonsRules)
                     {
                         if (pr.objRules.ID != -1)
                             _lstRules.Add(pr.objRules);

                     }

                    

                 }
                 
                 
                     
                 return _lstRules; }
             
         }



         List<Rules> _lstAllRules = null;
         List<long> _lstAllRulesIDs = null;

         public List<long> lstAllRulesIDs
         {
             get {
                 if (_lstAllRulesIDs == null)
                 {
                     lstAllRules.ToString();
                 }




                 return _lstAllRulesIDs;
             }
             
         }
         
         public List<Rules> lstAllRules
         {
             get {

                 if (_lstAllRules == null)
                 {
                     _lstAllRules = new List<Rules>();
                     _lstAllRules.AddRange(lstRules);

                     foreach (Groups gp in lstGroups)
                     {
                         _lstAllRules.AddRange(gp.lstRules);
                     }

                 }



                 _lstAllRules = new List<Rules>(from objRule in _lstAllRules
                                                group objRule by new { objRule.ID, objRule.RuleName, objRule.RuleNameEN , objRule.RuleDescriptionEN,objRule.RuleDescriptionAR } into MyGroup
                                                
                                                select new Rules (MyGroup.Key.ID,MyGroup.Key.RuleName,MyGroup.Key.RuleNameEN,MyGroup.Key.RuleDescriptionEN,MyGroup.Key.RuleDescriptionAR));


                 _lstAllRulesIDs = new List<long>(from objRule in _lstAllRules select objRule.ID);
                 
                 
                 return _lstAllRules; }
             
         }





         private List<PersonGroups> _lstPersonsGroups = null;

         public List<PersonGroups> lstPersonsGroups
         {
             get {

                 if (_lstPersonsGroups == null)
                 {
                     PersonGroupsDB objPersonGroupsDB = new PersonGroupsDB(ConnectionStrings.LMS);
                     List<PersonGroups> lstPersonGroups1 = objPersonGroupsDB.getPersonGroupsByPersonsID(GenID);
                     if (lstPersonGroups1.Count == 0)
                         return new List<PersonGroups>();


                     _lstPersonsGroups = lstPersonGroups1;
                 }
                 
                 
                 
                 
                 
                 
                 
                 return _lstPersonsGroups; }
             
         }



         private List<RegisteredCourses> _lstRegisteredCourses = null;

         public List<RegisteredCourses> lstRegisteredCourses
         {
             get {

                 if (_lstRegisteredCourses == null)
                 {
                     RegisteredCoursesDB objRegisteredCoursesDB = new RegisteredCoursesDB(ConnectionStrings.LMS);
                     _lstRegisteredCourses = objRegisteredCoursesDB.getRegisteredCoursesByEmployeesID(GenID);
                        
                 }
                 
                 
                 
                 
                 
                 
                 return _lstRegisteredCourses; }
             

         }


         private List<Courses> _lstCourses = null;

         public List<Courses> lstCourses
         {
             get {


                 if (_lstCourses == null)
                 {
                     _lstCourses = new List<Courses>();
                     CoursesDB objCoursesDB = new CoursesDB (ConnectionStrings.LMS);
                     foreach (RegisteredCourses obj in lstRegisteredCourses)
                     {
                         _lstCourses.AddRange(objCoursesDB.getCoursesByID(obj.CoursesID));

                     }
                 }


                 
                 
                 
                 
                 
                 return _lstCourses; }
             
         }





         List<Persons> _lstAddEmployees = null;

         public List<Persons> lstAddEmployees
         {
             get {

                 if (_lstAddEmployees == null)
                 {
                     ManagerEmployeesDB objManagerEmployeesDB = new ManagerEmployeesDB(ConnectionStrings.LMS);
                     List<ManagerEmployees> lstManagerEmployees = objManagerEmployeesDB.getManagerEmployeesByManagerID(GenID);
                     if (lstManagerEmployees.Count == 0)
                         return new List<Persons>();

                     PersonsDB objPersonsDB = new PersonsDB(ConnectionStrings.LMS);
                    
                     _lstAddEmployees = new List<Persons>();
                     foreach (ManagerEmployees obj in lstManagerEmployees)
                     {
                         List<Persons> lstPersons = objPersonsDB.getPersonsByGenID(obj.EmployeeID);
                         _lstAddEmployees.AddRange(lstPersons);
                     }
                 }
                                
                 
                 
                 
                 return _lstAddEmployees; }
             
         }

         List<Persons> _lstEmployees = null;

         public List<Persons> lstEmployees
         {
             get {

                 if (_lstEmployees == null)
                 {
                     //PersonsDB objPersonsDB = new PersonsDB(ConnectionStrings.LMS);
                     //List<Persons> lstPersons1 = objPersonsDB.Select("", " where [EmploymentStatus] <> '2' AND [PersonTypeID] = '1' AND [OrgChartID] = '" + OrgChartID.ToString() + "' ");

                     //getEmployeesByOrganizationChartRec(objOrgnizationChart, lstPersons1);
                    
                     //_lstEmployees = lstPersons1;

                     if (this.PersonTypeID != 2)
                         return new List<Persons>();


                     PersonsDB objPersonsDB = new PersonsDB(ConnectionStrings.LMS);

                     _lstEmployees = objPersonsDB.SelectManagerEmployees(this.GenID);

                 }
                 
                            
                 
                 
                 
                 
                 return _lstEmployees; }
             
         }


         /// <summary>
         /// this function will add all Employees with manager from the (obj parameter) childs
         /// </summary>
         /// <param name="obj">the parameter to get all child employees</param>
         /// <param name="lstAll">the list to add employees to</param>
         protected void getEmployeesByOrganizationChartRec(OrgnizationChart obj, List<Persons> lstAll)
         {
             PersonsDB objPersonsDB = new PersonsDB(ConnectionStrings.LMS);

             foreach (OrgnizationChart inobj in obj.lstChild)
             {
                 List<Persons> lstInPersons = objPersonsDB.Select("", " where  [EmploymentStatus] <> '2' AND [PersonTypeID] <> '3' AND [OrgChartID] = '" + inobj.ID.ToString() + "' ");
                 lstAll.AddRange(lstInPersons);

                 getEmployeesByOrganizationChartRec(inobj, lstAll);
             
             }
         }




         OrgnizationChart _objOrgnizationChart = null;

         public OrgnizationChart objOrgnizationChart
         {
             get {

                 if (_objOrgnizationChart == null)
                 {

                     OrgnizationChartDB objOrgnizationChartDB = new OrgnizationChartDB(ConnectionStrings.LMS);
                     List<OrgnizationChart> lstOrgnizationChart = new List<OrgnizationChart> ();
                     if (OrgChartID != null)
                        lstOrgnizationChart = objOrgnizationChartDB.getOrgnizationChartByID(OrgChartID.Value);

                     if (lstOrgnizationChart.Count == 0)
                         return new OrgnizationChart();

                     _objOrgnizationChart = lstOrgnizationChart[0];
                 
                 }
                 
                 
                 return _objOrgnizationChart; }
            
         }


         List<Persons> _lstAllEmployees = null;

         public List<Persons> lstAllEmployees
         {
             get {

                 if (_lstAllEmployees == null)
                 {
                     _lstAllEmployees = new List<Persons>();
                     _lstAllEmployees.AddRange(lstAddEmployees);
                     _lstAllEmployees.AddRange(lstEmployees);

                 }
                 
                 
                 
                 
                 
                 return _lstAllEmployees; }
             
         }




         Area _objMainArea = null;

         public Area objMainArea
         {
             get {

                 if (_objMainArea == null)
                 {
                     long CurrentAreaID = -1;
                     if (OperationAreaID == null)
                     {
                         CurrentAreaID = this.objParent.OperationAreaID.Value;
                     }
                     else
                         CurrentAreaID = OperationAreaID.Value;


                     AreaDB objAreaDB = new AreaDB(ConnectionStrings.LMS);

                     List<Area> lstArea = objAreaDB.getAreaByID(CurrentAreaID);

                     if (lstArea.Count == 0)
                         return new Area();

                     Area NewObjArea = lstArea[0];

                     while (NewObjArea.ParentID != null)
                     {
                         NewObjArea = NewObjArea.objParent;

                     }

                     _objMainArea = NewObjArea;

                 }
                 
                                
                 
                 return _objMainArea; }
            
         }



         List<Persons> _lstFamily = null;

         public List<Persons> lstFamily
         {
             get {

                 if (_lstFamily == null)
                 {
                     PersonsDB objPersonsDB = new PersonsDB(ConnectionStrings.LMS);
                     List<Persons> lstFamilyPersons = objPersonsDB.getPersonsByBadge(this.Badge);
                     lstFamilyPersons = new List<Persons>(from obj in lstFamilyPersons where obj.GenID != this.GenID select obj);
                     _lstFamily = lstFamilyPersons;
                 }         
                 
                 
                 return _lstFamily; }
         }




         bool? _isSaudi = null;

         public bool? isSaudi
         {
             get {

                 if (_isSaudi == null)
                 {
                      if (this.NationalityID == 177)
                         _isSaudi = true;
                     else
                         _isSaudi = false;

                 }


                 return _isSaudi;
             
             }
            
         }



         OrgnizationChart _objSector = null;

         public OrgnizationChart objSector
         {
             get {

                 if (_objSector == null)
                 {
                     Persons current = this;
                     if (this.objParent.GenID > -1)
                     {
                         current = this._objParent;
                     }

                     if (current.objOrgnizationChart.ChartTypeID <= 2)
                     {
                         _objSector = current.objOrgnizationChart;

                     }
                     else
                     {
                         OrgnizationChart obj = current.objOrgnizationChart;
                         while (obj.objParent.ID > 0 && obj.ChartTypeID > 2)
                         {
                             obj = obj.objParent;
                         }

                         _objSector = obj;


                     }
                    


                 }
                 
                 
                 
                 
                 
                 return _objSector; }
             
         }










         public void clearchilds()
         {
             
             _lstAllRules = null;
             _lstAllRulesIDs = null;
             _lstChilds = null;
             _lstGroups = null;
             _lstPersonsGroups = null;
             _lstRules = null;
             _lstAddEmployees = null;
             _lstEmployees = null;
             _lstAllEmployees = null;
             _lstCourses = null;
             _lstRegisteredCourses = null;
             _lstFamily = null;

             _isSaudi = null;
             _objSector = null;
        }







            

     }
}
